package cn.shop.dys.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPException;

import java.util.ArrayList;
import java.util.List;

import cn.shop.dys.R;
import cn.shop.dys.adapter.ChatAdapter;
import cn.shop.dys.bean.Msg;
import cn.shop.dys.util.ConnectionManager;
import cn.shop.dys.util.MyDBUtil;
import cn.shop.lib_common.CommonUtil;

/**
 *
 * 用户服务
 * Created by sunjian on 2016/4/18.
 *
 */
public class UserServiceActivity extends AppCompatActivity implements ConnectionManager.MsgObserver {
    @ViewInject(R.id.listview)
    private ListView mListView;
    @ViewInject(R.id.input)
    private EditText mContentET;
    private String chatJid;
    private List<Msg> mChatInfos=new ArrayList<>();
    private ChatAdapter mAdapter;
    private Chat chat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_service);
        ViewUtils.inject(this);
        chatJid = "xiaosi@sunjian.cn";
        MyDBUtil.clearNewMsgCountByJid(chatJid);
        chat = ConnectionManager.getInstance().getChatManager().createChat(chatJid, null);
         ConnectionManager.getInstance().addChatMsgObserver(chatJid,this);
        init();
        findViewById(R.id.iv_service_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ConnectionManager.getInstance().removeChatMsgObserver(chatJid);
        MyDBUtil.deleteMsgByJid(chatJid);
    }

    @Override
    protected void onResume() {
        super.onResume();
        List<Msg> msgs = MyDBUtil.querySingleByJid(chatJid);
        if(msgs==null||msgs.size()==0)return;
        mChatInfos.addAll(msgs);
        setAdapter();
    }

    private void init() {
    }

    private void setAdapter() {
        //     CommonUtil.showToast(this,mChatInfos.toString());
        //   roster = ConnectionManager.getInstance().getRoster();
        if(mAdapter==null){
            mAdapter = new ChatAdapter(this, mChatInfos);
            mListView.setAdapter(mAdapter);
        }else {
            mAdapter.notifyDataSetChanged();
        }
        mListView.setSelection(mAdapter.getCount());
    }
    public void send(View view){
        String mContent = mContentET.getText().toString().trim();
        if(TextUtils.isEmpty(mContent)){
            return;
        }
        Msg msg = Msg.creatMsg(chatJid, mContent, Msg.CHAT_TYPE_ME);
        mChatInfos.add(msg);
        try {
            chat.sendMessage(msg.content);
        } catch (XMPPException e) {
            e.printStackTrace();
        }
        setAdapter();
        MyDBUtil.saveMsg(msg);
        mContentET.setText("");
    }

    @Override
    public void notify(Msg msg) {
        if(msg==null)return;
        MyDBUtil.clearNewMsgCountByJid(msg.jid);
        mChatInfos.add(msg);
        CommonUtil.runOnUIThread(new Runnable() {
            @Override
            public void run() {
                setAdapter();
            }
        });
    }
}
